<?php

class searchSubApp extends application {

    public $defaultPage = 'search';

    public function init(){

        # Pagina's die we kunnen openen
        $this->addPage('search','pageSearch();');
        $this->addPage('searchResult','pageSearchResult();');
        $this->addPage('showmap','pageShowMap();');

        parent::init();

    }

    public function pageSearch() {

        $page = $this->createMainPage('ITract.nl', 'homepage');

        return $page;

    }

    public function pageSearchResult() {

        $page = $this->createMainPage('Gevonden routes', 'subpage');
        $page->append('Gevonden routes <br/><br/>','pagetitle');

        $route = $_SESSION['searchresponse'];
        
        if($route->ritten == '' || count($route->ritten) == 0) {
            $page->append('Er ging iets mis, probeer opnieuw te zoeken','content');
        } else {

            $page->appendTemplate('rititem','content',true);
            $page->append('<b>Van</b>','start',true);
            $page->append('<b>Naar</b>','eind',true);
            $page->append('<b>Tijd</b>','datum',true);
            
            foreach($route->ritten as $route) {
                
                $page->appendTemplate('rititem','content',true);
                $page->append($route->start,'start',true);
                
                if(isset($route->waypoint)) {
                    $page->append($route->waypoint, 'eind', true);
                } else {
                    $page->append($route->eind,'eind',true);
                }
                
                $page->append($route->tijd,'datum',true);
                $page->append('<a href="index.php?application=search&page=showmap&van='.$route->start.'&naar='.$route->eind.'&waypoint='.$route->waypoint.'" class="openModal">Openen</a>','button',true);
                
                if(isset($route->waypoint)) {
                    
                    $page->append('Deze route bevat een vervolgitem','header',true);
                    
                    $vertrektijd = date('Y-m-d H:i:s', strtotime($route->extrareisinformatie->vertrektijd));
                    
                    $page->append($route->waypoint,'startOV',true);
                    $page->append($route->eind,'eindOV',true);
                    //$page->append($vertrektijd,'datumOV',true);
                    
                }
                
            }
            
        }
        
        return $page;

    }

    public function pageShowMap() {
        
        $page = $this->createMainPage('ITrecht.nl Gevonden routes', 'mapiframe');
        $page->append('Gevonden routes','pagetitle');

        $page->addJavascriptFile('maps');

        $van = str_replace('<br/>',' ',$_GET['van']);
        $naar = str_replace('<br/>',' ',$_GET['naar']);
        $waypoint = str_replace('<br/>',' ',$_GET['waypoint']);
        
        $page->appendTemplate('starteindpunt','map');
        $page->append($van,'start');
        $page->append($naar,'eind');
        $page->append($waypoint,'waypoint');
        
        $page->appendTemplate('mapscript','javaHead');
        $page->appendTemplate('maps','map');
        $page->assign($GLOBALS['googleMapsV3Key'],'googleMapKey');
        $page->assign('onload="initialize()"','bodyJava');

        return $page;
        
    }
    
}

?>
